package com.gadgets.busi.bm02.common;

import com.gadgets.busi.bm02.pojo.MaterialDict;
import com.gadgets.busi.bm02.repository.IMaterialDictRepository;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.start.AbsCacheInitLoaderProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-11-02
 * 
 */
@Component
public class GadgetsBm02StartProcessor extends AbsCacheInitLoaderProcess {

    @Autowired
    private IMaterialDictRepository repository;
    @Autowired
    private GadgetsBm02Utils bm02Utils;

    public GadgetsBm02StartProcessor() {
        super("2");
    }

    /**
     * 初始化执行方法
     */
    @Override
    public void initLoad() {
        logger.info("开始加载MC物品字典表");
        // 启动阶段分页查询加载物品字典
        int i = 1;
        Pagination pagination = new Pagination();
        pagination.setPageSize(100);
        pagination.setCurrentPage(i);
        PaginationUtil.bulidPaginationInfo(pagination);
        List<MaterialDict> materialDicts = repository.selectAllMaterialDict();
        PaginationUtil.getPaginationResultInfo(materialDicts,pagination);
        putCacheItem(materialDicts);
        int totalPage = pagination.getTotalPage();
        while (i<totalPage){
            i++;
            pagination.setCurrentPage(i);
            PaginationUtil.bulidPaginationInfo(pagination);
            materialDicts = repository.selectAllMaterialDict();
            PaginationUtil.getPaginationResultInfo(materialDicts,pagination);
            putCacheItem(materialDicts);
        }
        logger.info("MC物品字典表加载完成");
    }

    protected void putCacheItem(List<MaterialDict> materialDicts){
        for (MaterialDict materialDict : materialDicts) {
            bm02Utils.addMcMeterialDictItem(materialDict);
        }
    }

}
